package com.niodata.dp.core.security;

import com.niodata.dp.core.resource.AbstractResourceDetailResolver;
import com.niodata.dp.core.resource.CombinedResourceDetailResolver;
import com.niodata.dp.core.resource.ResourceDetailResolver;
import java.util.Map;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class CombinedResourceDetailResolverFactoryBean implements
      FactoryBean<CombinedResourceDetailResolver> {

  private final CombinedResourceDetailResolver resolver = new CombinedResourceDetailResolver();
  @Autowired
  private ApplicationContext context;

  @Override
  public CombinedResourceDetailResolver getObject() throws Exception {
    Map<String, AbstractResourceDetailResolver> map = context
          .getBeansOfType(AbstractResourceDetailResolver.class);
    for (ResourceDetailResolver detailResolver : map.values()) {
      resolver.registerResolver(detailResolver);
    }
    return resolver;
  }

  @Override
  public Class<?> getObjectType() {
    return CombinedResourceDetailResolver.class;
  }

  @Override
  public boolean isSingleton() {
    return true;
  }
}
